<template>
  <div class="basic-info">
    <div class="basic-body common-content"
         v-loading="!show"
         element-loading-text="拼命加载中"
         element-loading-background="rgba(0, 0, 0, 0)">
      <el-scrollbar style="height: 100%;">
        <div class="scrollbar-box">
          <div class="comp-item-group" v-if="show">
            <div class="comp-item-box">
              <div class="comp-item-title">填写应用信息</div>
              <div class="comp-item-cont">
                <el-form
                    :model="basicForm"
                    :rules="basicRules"
                    ref="basicForm"
                    size="small"
                    label-width="100px"
                    class="demo-ruleForm2">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="应用名称" prop="appName">
                        <el-input v-model="basicForm.appName" placeholder="请输入应用名称"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用版本号" prop="appVersion">
                        <el-input v-model="basicForm.appVersion" placeholder="请输入应用版本号"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用类型" prop="appType">
                        <el-select
                            class="w100"
                            v-model="basicForm.appType"
                            placeholder="选择应用类型">
                          <el-option label="单组件应用" :value="1"></el-option>
                          <el-option label="组合应用" :value="2"></el-option>
                          <el-option label="单执行应用" :value="3"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="basicForm.appType===1">
                      <el-form-item label="应用组件" prop="appVersion">
                        <el-input
                            :value="basicForm.pluginsName||basicForm.pluginsId"
                            placeholder="请选择应用组件"
                            readonly
                            @click.native="showPluginsDailog"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="数据入库" prop="appIndb">
                        <el-select
                            class="w100"
                            v-model="basicForm.appIndb"
                            placeholder="数据是否入库">
                          <el-option label="入库" :value="0"></el-option>
                          <el-option label="不入库" :value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="basicForm.appIndb===0">
                      <el-form-item label="数据库名称" prop="appVersion">
                        <el-input v-model="basicForm.appDBName" placeholder="请输入数据库名称"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="所属资源" prop="resourceId">
                        <el-select
                            class="w100"
                            v-model="basicForm.resourceId"
                            placeholder="选择所属资源">
                          <el-option
                              v-for="(item, index) in resource"
                              :key="index"
                              :label="item.resName"
                              :value="item.id"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否监听" prop="isMonitor">
                        <el-select
                            class="w100"
                            v-model="basicForm.isMonitor"
                            placeholder="数据是否监听">
                          <el-option label="监听" value="0"></el-option>
                          <el-option label="不监听" value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否切换" prop="isSwitch">
                        <el-select
                            class="w100"
                            v-model="basicForm.isSwitch"
                            placeholder="数据是否切换">
                          <el-option label="切换" value="0"></el-option>
                          <el-option label="不切换" value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="版本说明" prop="appRemarks">
                        <el-input
                            type="input"
                            v-model="basicForm.appRemarks" placeholder="请输入版本说明"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="场景描述" prop="appScene">
                        <el-input
                            type="textarea"
                            v-model="basicForm.appScene" placeholder="请输入场景描述"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">填写执行计划</div>
              <div class="comp-item-cont">
                <el-form
                    :model="planForm"
                    :rules="planRules"
                    ref="planForm"
                    size="small"
                    label-width="100px"
                    class="demo-ruleForm2">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="计划名称" prop="planName">
                        <el-input v-model="planForm.planName" placeholder="请输入计划名称"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="计划类型" prop="executeType">
                        <el-select
                            class="w100"
                            v-model="planForm.executeType"
                            placeholder="请选择计划类型">
                          <el-option label="即时执行" :value="0"></el-option>
                          <el-option label="常驻进程" :value="1"></el-option>
                          <el-option label="周期执行" :value="2"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="执行单元" prop="executeUnit">
                        <el-select
                            class="w100"
                            v-model="planForm.executeUnit"
                            placeholder="请选择执行单元">
                          <el-option label="每天" value="0"></el-option>
                          <el-option label="每周" value="1"></el-option>
                          <el-option label="每月" value="2"></el-option>
                          <el-option label="每年" value="3"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="时间" prop="executeTime">
                        <el-input v-model="planForm.executeTime" placeholder="请输入执行时间"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="最小周期" prop="executeDay">
                        <el-input v-model="planForm.executeDay" placeholder="请输入执行最小周期"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="备注" prop="remarks">
                        <el-input v-model="planForm.remarks" placeholder="请输入备注"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">录入运行环境</div>
              <div class="comp-item-cont">
                <el-form
                    :model="environmentForm"
                    :rules="environmentRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px"
                    class="demo-ruleForm2">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="使用系统" prop="systemId">
                        <el-select
                            class="w100"
                            v-model="environmentForm.systemId"
                            placeholder="请选择使用系统">
                          <el-option
                              v-for="item in systemOptions"
                              :key="item.id"
                              :label="item.systemName"
                              :value="item.id">
                          </el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小CPU">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireCPU"
                            placeholder="请输入最小CPU"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小存储单元">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireDisk"
                            placeholder="请输入最小存储单元"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小GPU">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireGPU"
                            placeholder="请输入最小GPU"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="运行内存要求">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireMemory"
                            placeholder="请输入运行内存要求"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item class="up" label="上传环境">
                        <el-input v-model="environmentForm.requirePath" placeholder="请上传运行环境">
                          <el-upload
                              slot="append"
                              class="upload-demo"
                              action="#"
                              :show-file-list="false"
                              :http-request="environmentFile">
                            <el-button size="small" type="primary" :loading="fileUpLoading">点击上传</el-button>
                          </el-upload>
                        </el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="备注" prop="remarks">
                        <el-input
                            type="input"
                            v-model="environmentForm.remarks" placeholder="请输入备注"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box" v-if="basicForm.isMonitor==='0'">
              <div class="comp-item-title">配置应用监控</div>
              <div class="comp-item-cont">
                <el-form
                    :model="monitorForm"
                    :rules="monitorRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px"
                    class="demo-ruleForm2">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="监控频率/s">
                        <el-input v-model="monitorForm.monitorRate" placeholder="请输入监控频率/s"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="监控类型" prop="monitorType">
                        <el-select
                            class="w100"
                            v-model="monitorForm.monitorType"
                            placeholder="请选择监控类型"
                            @change="changeMonitorType">
                          <el-option label="全部" :value="2"></el-option>
                          <el-option label="进程监控" :value="0"></el-option>
                          <el-option label="数据监控" :value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="monitorForm.monitorType!==''">
                      <el-form-item label="监控类型">
                        <div style="line-height: 1;">
                          <MonitorList
                              ref="monitorList"
                              :data="paroles"></MonitorList>
                        </div>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box" v-if="basicForm.isSwitch==='0'">
              <div class="comp-item-title">应用故障切换</div>
              <div class="comp-item-cont">
                <el-form
                    :model="switchForm"
                    :rules="switchRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px"
                    class="demo-ruleForm2">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="应用切换名称">
                        <el-input v-model="switchForm.switchName" placeholder="请输入应用切换名称"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用切换地址">
                        <el-input v-model="switchForm.switchAddress" placeholder="请输入应用切换地址"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-btn-group">
              <el-button type="primary" size="small" @click="ediBtn" :loading="btnLoading">保存修改</el-button>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>

    <PluginsDailog ref="pluginsDailog" @checked="checkedPlugin"></PluginsDailog>

  </div>
</template>

<script>
import {
  get_getResource,
  get_getAppliesInfo,
  put_editAApplication
} from "@/apis/appLC/applc";
import {
  get_getSystem,
} from "@/apis/assembly/assembly";
import MonitorList from '../basic/comp/MonitorList'
import PluginsDailog from '../basic/comp/PluginsDailog'

export default {
  name: 'updateApp',
  components: {MonitorList, PluginsDailog},
  data () {
    return {
      show: false,
      // 基本信息表单
      basicForm: {
        appName: "",
        appVersion: "",
        appIndb: "0",
        appDBName: "",
        isMonitor: "0",
        isSwitch: "1",
        appRemarks: "",
        remarks: "",
        resourceId: "",
        appScene: "",
        appType: "",
        id: "",
        pluginsId: '',
        pluginsName: ''
      },
      //基本信息验证
      basicRules: {},
      planForm: {
        planName: "",
        executeType: "",
        executeUnit: "",
        executeTime: "",
        executeDay: "",
        remarks: "",
        id: ""
      },
      planRules: {},
      //配置监控表单
      monitorForm: {
        monitorRate: "",
        monitorType: "",
        pId: "",
        id: ""
      },
      //监控表单校验
      monitorRules: {},
      //录入环境表单
      environmentForm: {
        pId: "",
        systemId: "",
        requireCPU: "",
        requireDisk: "",
        requireGPU: "",
        requireMemory: "",
        remarks: "",
        id: "",
        requirePath: "",
        file: ""
      },
      environmentRules: {},
      // 组件切换表单---------------------------
      switchForm: {
        switchAddress: "",
        switchName: "",
        pId: "",
        id: ""
      },
      switchRules: {},
      resource: [], //资源外键
      appId: "",
      isAddApp: false,
      systemOptions: [],
      paroles: [], // 监控规则
      fileUpLoading: false,
      btnLoading: false
    };
  },
  created () {
    this.getResource();
    this.getSystem();
    this.getDetails();
  },
  mounted () {
  },
  methods: {
    getDetails () {
      get_getAppliesInfo({appId: this.$route.query.appId}).then((res) => {
        let {applies, aplan, pamonitor, parequire, paswitch} = res.data.data;
        this.basicForm = Object.assign(this.basicForm, applies)
        if (paswitch) {
          this.switchForm.switchAddress = paswitch.switchAddress
          this.switchForm.switchName = paswitch.switchName
          this.switchForm.pId = paswitch.pId
          this.switchForm.id = paswitch.id
        } else {
          this.switchForm.pId = applies.id;
        }
        if (aplan) {
          for (const key in this.planForm) {
            this.planForm[key] = aplan[key];
          }
        }
        if (pamonitor) {
          for (const key in this.monitorForm) {
            this.monitorForm[key] = pamonitor[key];
          }
          this.monitorForm.pId = applies.id;
        }

        if (parequire) {
          for (const key in this.environmentForm) {
            this.environmentForm[key] = parequire[key];
          }
          this.environmentForm.pId = applies.id;
          this.environmentForm.file = parequire.requirePath;
        }

        if (this.monitorForm.monitorType) {

        }
        if (pamonitor && pamonitor.paroles.length) {
          this.paroles = pamonitor.paroles.map(item => {
            return {
              name: item.roleType == 1 ? '数据监控' : '进程监控',
              roleType: item.roleType,
              roleName: item.roleName,
              roleProcessName: item.roleProcessName
            }
          })
        } else {
          // 生成监控规则列表
          this.createParoles(this.monitorForm.monitorType)
        }

        this.show = true
      });
    },
    showPluginsDailog () {
      this.$refs.pluginsDailog.show()
    },
    checkedPlugin (item) {
      this.basicForm.pluginsId = item.id
      this.basicForm.pluginsName = `${item.pluginsName} (${item.pluginsVesion})`
    },
    // 录入基本信息组件
    getFile (val) {
      let data = new FormData();
      data.append("file", val.file);
    },
    // 录入环境信息上传文件
    environmentFile (data) {
      this.fileUpLoading = true
      let formData = new FormData()
      formData.append('file', data.file)
      formData.append('id', '')
      this.$api.editPARequireFile(formData).then(res => {
        if (res.data.code === 0) {
          this.environmentForm.requirePath = res.data.data
        }
      }).finally(() => {
        this.fileUpLoading = false
      })
    },
    // 系统下拉
    getSystem () {
      get_getSystem().then((res) => {
        this.systemOptions = res.data.data;
      });
    },
    // 获取资源外键
    getResource () {
      get_getResource().then((res) => {
        this.resource = res.data.data;
      });
    },
    createParoles (type) {
      if (type == 2) {
        this.paroles = [
          {name: '进程监控', roleType: 0, roleName: '', roleProcessName: '', remarks: ''},
          {name: '数据监控', roleType: 1, roleName: '', roleProcessName: '', remarks: ''}
        ]
      } else if (type == 1) {
        this.paroles = [
          {name: '数据监控', roleType: 1, roleName: '', roleProcessName: '', remarks: ''}
        ]
      } else if (type == 0) {
        this.paroles = [
          {name: '进程监控', roleType: 0, roleName: '', roleProcessName: '', remarks: ''}
        ]
      }
    },
    changeMonitorType (val) {
      this.createParoles(val)
    },
    // 保存修改
    ediBtn () {
      let monitorDatas = this.$refs.monitorList && this.$refs.monitorList.lists

      let opt = {
        aPlan: this.planForm,
        pRequire: this.environmentForm,
        Applies: this.basicForm,
      };

      opt.aPlan.paId = this.basicForm.id
      opt.pRequire.paId = this.basicForm.id
      if (this.basicForm.isSwitch == 0) {
        opt.pSwitch = this.switchForm
        opt.pSwitch.paId = this.basicForm.id
      } else {
        opt.pSwitch = null
      }
      if (this.basicForm.isMonitor == 0) {
        opt.pMonitor = this.monitorForm
        opt.pMonitor.paId = this.basicForm.id
      } else {
        opt.pMonitor = null
      }

      if (this.basicForm.isMonitor == 0) {
        opt.roles = monitorDatas ? monitorDatas.map(item => {
          return {
            monitorId: this.monitorForm.id,
            roleType: item.roleType,
            roleName: item.roleName,
            roleProcessName: item.roleProcessName
          }
        }) : []
      } else {
        opt.roles = null
      }

      this.btnLoading = true
      put_editAApplication({...opt}).then((res) => {
        let {msg, code} = res.data;
        this.$message.success(msg);
        this.$router.push({
          name: "AppArrange",
          query: {add: "add", appid: this.$route.query.appId},
        });
      }).finally(() => {
        this.btnLoading = false
      });
    }
  }
};
</script>
<style lang="less" scoped>
  .scrollbar-box {
    padding: 15px;
  }

  .comp-item-group {
    .comp-item-box {
      margin-bottom: 15px;

      .comp-item-title {
        height: 40px;
        color: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: rgba(3, 68, 108, 0.8);
        font-size: 17px;
        font-weight: bold;
      }

      .comp-item-cont {
        padding: 15px;
        border: solid 1px rgba(3, 68, 108, 0.5);
      }
    }

    .comp-item-btn-group {
      text-align: center;
      margin-top: 15px;
    }

    /deep/ .el-input-group__append {
      background-color: #66b1ff;
      border-color: #66b1ff;
      color: #ffffff;
    }
  }

  .basic-info {
    height: 100%;
    width: 100%;

    .basic-body {
      height: 100%;
      width: 100%;
      overflow: hidden;
    }

    .cont-bottom {
      background: white;
      height: 40px;
      line-height: 40px;
      padding: 0 15px;
    }

    /deep/ .el-collapse-item__header {
      position: relative;
    }
  }


  /deep/ .el-form-item--small .el-form-item__content {
    line-height: 33px;
  }

  .el-pagination {
    position: relative;
    white-space: nowrap;
    padding: 10px 10px;
    font-weight: 700;

    /deep/ .el-pagination__total {
      color: #ffffff;
    }

    /deep/ .el-pagination__jump {
      color: #ffffff;
    }
  }
</style>
